package net.qhzw.vip.qiyu.controller;

import cn.hutool.core.util.ZipUtil;
import io.minio.MinioClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.qhzw.vip.qiyu.config.FileTemplate;
import net.qhzw.vip.qiyu.handler.MyException;
import net.qhzw.vip.qiyu.result.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("file")
@Api(tags = "文件控制器")
public class FileController {

    @Autowired
    private FileTemplate fileTemplate;

    @ApiOperation(value = "上传文件")
    @PostMapping("upload")
    public JsonResult upload(@RequestParam("file") MultipartFile file) {
        String upload = fileTemplate.fileUpload(file);
        return JsonResult.ok(upload);
    }

    @ApiOperation(value = "上传文件")
    @PostMapping("uploadRep")
    public JsonResult uploadRep(@RequestParam("file") MultipartFile file,String name) {
        if (name.length() < 0 && name.isEmpty() && name == null  ){
            throw new MyException("请检查name 是否正确！！！");
        }
        String upload = fileTemplate.fileUpload1(file,name);
        return JsonResult.ok(upload);
    }

    /**
     * @description: 批量下载
     * @date: 2022/8/17 16:50
     * @param: filenames: 多个文件名称
     * @Param: zip: 压缩包名称
     * @Param: res: 响应对象
     * @return: void
     **/

    //@ApiOperation(value = "下载照片压缩文件")
    //@PostMapping("batchDownload")
    //public JsonResult batchDownload(List<String> filenames, String zip, HttpServletResponse res, HttpServletRequest req) {
    //
    //    fileTemplate.batchDownload(filenames,zip,res,req);
    //    return JsonResult.ok();
    //}

    @ApiOperation(value = "上传文件")
    @PostMapping("upload2")
    public String upload2(@RequestParam("file") MultipartFile file) {
        String upload = fileTemplate.fileUpload(file);
        return upload;
    }

    @ApiOperation("文件删除")
    @DeleteMapping(value = "delete")
    public JsonResult delete(@RequestParam("fileName") String fileName) {
        fileName = fileName.substring(fileName.indexOf("/") + 1, fileName.length());
        boolean isOK = fileTemplate.delete(fileName);
        if (isOK) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }

    @ApiOperation(value = "多文件上传")
    @PostMapping("uploads")
    public JsonResult upload(@RequestParam("files") MultipartFile[] files) {
        List<String> result = fileTemplate.fileUploads(files);
        return JsonResult.ok(result);
    }





}